package com.gin.kotlin.gf2gamecommunity.composable

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import com.gin.kotlin.gf2gamecommunity.R
import com.gin.kotlin.gf2gamecommunity.base.composable.ConfirmDialog
import com.gin.kotlin.gf2gamecommunity.base.composable.MenuItemButton
import com.gin.kotlin.gf2gamecommunity.base.util.clearClip
import com.gin.kotlin.gf2gamecommunity.base.util.getClipText
import com.gin.kotlin.gf2gamecommunity.navi.GF2Navigator

@Composable
fun OpenLinkButton(navigator: GF2Navigator, menuItem: Boolean = false) {
    val context = LocalContext.current
    var clipText by remember { mutableStateOf("") }
    var showNaviDialog by remember { mutableStateOf(false) }

    Box {
        MenuItemButton(
            text = "打开链接",
            menuItem = menuItem,
            icon = { Icon(painterResource(R.drawable.ic_link_24), contentDescription = "打开链接") },
            onClick = {
                clipText = context.getClipText() ?: ""
                showNaviDialog = navigator.openable(clipText)
            })

        if (showNaviDialog) ConfirmDialog(
            thirdButtonText = "清空",
            thirdButtonOnclick = { showNaviDialog = false; context.clearClip() },
            onDismissRequest = { showNaviDialog = false },
            onConfirmation = { showNaviDialog = false; navigator.onNavigateByUrl(clipText) }
        ) {
            Column {
                Text("发现剪贴板中的链接，是否打开？")
                Text(clipText)
            }
        }
    }
}